/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heartinjava;
import java.net.URL;
import java.security.SecureRandom;
import java.util.ArrayList;
import javax.swing.ImageIcon;

/**
 *
 * @author TIENDAT
 */

public class ComputerPlayer extends Play {
    
    public static class ThreeCard 
    {
        private int c1;     // la bai 1
        private int c2;     // la bai 2
        private int c3;     // la bai 3

        public ThreeCard() 
        {
            c1 = c2 = c3 = -1;
        }

        public ThreeCard(int playARandomCard, int playARandomCard0, int playARandomCard1) 
        {
            c1 = playARandomCard;
            c2 = playARandomCard0;
            c3 = playARandomCard1;
        }

        public int getC1() 
        {
            return c1;
        }

        public void setC1(int c1) 
        {
            this.c1 = c1;
        }

        public int getC2() 
        {
            return c2;
        }

        public void setC2(int c2) 
        {
            this.c2 = c2;
        }

        public int getC3() 
        {
            return c3;
        }

        public void setC3(int c3) 
        {
            this.c3 = c3;
        }
    }
    
    private ArrayList<Integer> mangBaLB;
    private int delay = 100;
    private ArrayList<ThreeCard> lstMangBaLB;
    
    public ComputerPlayer(HeartInJava m_main, KhungBanCo g)
    {    
        player = new Player[4];
        player[0] = new Player("You");
        player[1] = new Player("Player 2");
        player[2] = new Player("Player 3");
        player[3] = new Player("Player 4");
        arrayId4Card = new ArrayList<Integer>();
        
        this.m_Main = m_main;
        this.gui = g;
    }

    @Override
    public void Update(){
        if(playState == KhaiBao.PLAYSTART){
            disableButton();
            chiaBai();
            System.out.println("Switch to Game Exchange !!! ");
            if (countSoVanBaiChoi % 4 != 3)
            {   // bien nay de dem so ban chơi và dc cộng lên 1 khi qua ván khác 
                // nhung dc cộng thêm 1 khi nó %4 !=3
                playState = KhaiBao.PLAYEXCHANGE;
            }
            else {
                flagDoiBai = true;
                playState = KhaiBao.PLAYPLAYING;
                btnXuLyChung.setVisible(false);
            }
        }
        else if(playState == KhaiBao.PLAYEXCHANGE){
            // cho nguoi choi doi bai
            if (!flagDoiBai)
            {
                ReceiveExchange();
            }
           
        } else if (playState == KhaiBao.PLAYPLAYING) {
            if (clickLaBai != -1) {
                if (currentTurn == 0)
                {
                    ReceiveCardPlay();
                }
                clickLaBai = -1;
            }
            KiemTraDu4LaBai();
        }
        else if(playState == KhaiBao.PLAYEND){
        }
    }
  
    @Override
    public void ReceiveCardPlay() {
        super.ReceiveCardPlay(); 
        KiemTraDu4LaBai();
        computerPlayPoker();
    }
    
    //-------------------------------
    //-- 
    //-------------------------------
    private boolean KiemTraDu4LaBai() 
    {
        if (arrayId4Card.size() == 4) {
            System.out.println("Checking end 4 card ...");
            int winCard = KiemTra4LaBaiWin();
            for (int i = 0; i < 4; i++) {
                if (arrayId4Card.get(i) == winCard) {
                    firstturn = (firstturn + i) % 4;
                    player[firstturn].congDiem4LabaiDanh(arrayId4Card);
                    capNhatDiem();
                    currentTurn = firstturn;
                    if (firstturn == 0) {
                        this.notice("Đến nước chơi của bạn đó ^ ^ ...");
                    } else {
                        this.notice("Chờ người " + (firstturn + 1) + " đánh ...");
                    }
                    break;
                }
            }
            try {
                Thread.sleep(1000);
            }catch (Exception ex){}
            count13Card++;
            
            arrayId4Card.clear();
            hienTatCaLaBaiChay();
            
            // kiem tra danh ván bài mới
            if (count13Card == 13) 
            {
                KetThucMotVanBai();
                
                dem = 0;                
                btnXuLyChung.setIcon(null);
                btnXuLyChung.setBorderPainted(true);
                btnXuLyChung.setContentAreaFilled(true);
                btnXuLyChung.setFocusPainted(true);
                btnXuLyChung.setBounds(400, 300, 100, 30);
                
                btnXuLyChung.setVisible(true);
                btnXuLyChung.setEnabled(true);
                btnXuLyChung.setText("Đổi bài");
                count13Card = 0;
                countSoVanBaiChoi++;
                //rip a dat
            }
            return true;
        }
        return false;
    }
    
    //----------------------------
    //-- Computer tự đánh
    //----------------------------
    public void computerPlayPoker() 
    {
        try {
            Thread.sleep(20);
        } catch (InterruptedException ex) {}
        
        while ((currentTurn != 0) && (playState == KhaiBao.PLAYPLAYING)) 
        {
            if (currentTurn == firstturn) 
            {
                if (count13Card != 0) 
                {
                    System.out.println("Player[" + currentTurn + "] dsBai: " + player[currentTurn].getListCard());
                    arrayId4Card.add(player[currentTurn].playfirst(flagChonCo));
                }
                else 
                {
                    arrayId4Card.add(player[currentTurn].di2Chuong());
                }
            } 
            else 
            {
                arrayId4Card.add(player[currentTurn].playfollow(arrayId4Card.get(0)));    
                // Neu nguoi choi di dau tien di quan ko phai la quan Co, ma nhung nguoi sau do lai di quan co (do bai nguoi do da het nuoc)
                // nếu hết nước thì đi thằng khác 
                if (LaBai.getnuoc_labai(arrayId4Card.get(0)) != KhaiBao.NUOCHEARTS
                        && LaBai.getnuoc_labai(arrayId4Card.get(arrayId4Card.size() - 1)) == KhaiBao.NUOCHEARTS) {
                    flagChonCo = true;
                    this.notice("Trái tim bị vỡ !!!!!");
                    dem++;
                    if(dem == 1)
                    {
                        //-- hiện trái tim
                        URL path = getClass().getResource("resources/traitim1.png");
                        icon=new ImageIcon(path);
                        btnXuLyChung.setVisible(true);
                        btnXuLyChung.setIcon(icon);
                        this.btnXuLyChung.setBounds(405, 255, 90, 90);
                        btnXuLyChung.setEnabled(true);
                        btnXuLyChung.setBorderPainted(false);
                        btnXuLyChung.setContentAreaFilled(false);
                        btnXuLyChung.setFocusPainted(false);
                        btnXuLyChung.setText(null);
                        //--end
                    }
                    System.out.println("dem = " + dem);
                    System.out.println("Trái tim bị vỡ !!!!!");
                }
            }
            hienTatCaLaBaiChay();
            try {
                Thread.sleep(delay);
            } catch (Exception e) {}
            
           if (!KiemTraDu4LaBai())
               nextturn();
           System.out.println("Current turn: " + currentTurn + "; Player[" + currentTurn +"] dsBai = " + player[currentTurn].getListCard().size());
           if (player[currentTurn].getListCard().isEmpty())
           {
               playState = KhaiBao.PLAYSTART;
               break;
           }
        }
    }
    
      
    //-----------------------------------------
    //-- xử lý việc chọn đổi bài
    //-- nếu đủ 3 lá thì hiện button cho đổi bài
    //-----------------------------------------
    @Override
    protected void ReceiveExchange() {
        if (clickLaBai != -1) 
        {
            System.out.println("Click to card " + clickLaBai);
            if (player[0].getThreeCard().contains(clickLaBai)) {
                for (int i = 0; i < player[0].getThreeCard().size(); i++) {
                    if (player[0].getThreeCard().get(i) == clickLaBai) {
                        player[0].getThreeCard().remove(i);
                        break;
                    }
                }
                LaBaiChuaDuocChon(clickLaBai);
            }
            else if (player[0].getThreeCard().size() != 3) 
            {
                player[0].getThreeCard().add(clickLaBai);
                LaBaiDaDuocChon(clickLaBai);
            }
            clickLaBai = -1;
        }

        if (player[0].getThreeCard().size() == 3) 
        {
            showbutton("Đổi bài");
            enableExchange();
        } else {
            disableButton();
        }
    }

    @Override
    //----------------------------------------------
    //-- Tiến hành đổi bài
    //-- countSoVanBaiChoi % 4 = 0: đổi ben phai
    //-- countSoVanBaiChoi % 4 = 1: đổi doi dien
    //-- countSoVanBaiChoi % 4 = 2: đổi ben trai
    //-- countSoVanBaiChoi % 4 = 3: ko đổi
    //----------------------------------------------
    protected void DoExchange() {
        if ((countSoVanBaiChoi % 4) != 3) {
            mangBaLB = player[0].getThreeCard();
            
            // Luu mot mang cacs phan tu ThreeCard (1 ThreeCard se chua 3 la bai) cua computer1, com2, com3
            // Index 0: player[0]
            // Index 1: player[1]
            // Index 2: player[2]
            // Index 2: player[3]            
            lstMangBaLB = new ArrayList<ThreeCard>();
            lstMangBaLB.add(player[0].playThreeCard());
            for (int i = 1; i < 4; ++i) {
                // Tao doi tuong chua 3 la bai 
                ThreeCard _3cards = new ThreeCard(player[i].danhMotLaBaiNgauNhien(), player[i].danhMotLaBaiNgauNhien(), player[i].danhMotLaBaiNgauNhien());
                lstMangBaLB.add(_3cards);
            }

            switch (countSoVanBaiChoi % 4) {
                // Vong 1:
                case 0:
                    player[0].nhanThemLaBai(lstMangBaLB.get(1).getC1());
                    player[0].nhanThemLaBai(lstMangBaLB.get(1).getC2());
                    player[0].nhanThemLaBai(lstMangBaLB.get(1).getC3());
                    player[1].nhanThemLaBai(lstMangBaLB.get(0).getC1());
                    player[1].nhanThemLaBai(lstMangBaLB.get(0).getC2());
                    player[1].nhanThemLaBai(lstMangBaLB.get(0).getC3());
                    
                    player[2].nhanThemLaBai(lstMangBaLB.get(3).getC1());
                    player[2].nhanThemLaBai(lstMangBaLB.get(3).getC2());
                    player[2].nhanThemLaBai(lstMangBaLB.get(3).getC3());
                    player[3].nhanThemLaBai(lstMangBaLB.get(2).getC1());
                    player[3].nhanThemLaBai(lstMangBaLB.get(2).getC2());
                    player[3].nhanThemLaBai(lstMangBaLB.get(2).getC3());
                    break;

                case 1:
                    player[0].nhanThemLaBai(lstMangBaLB.get(3).getC1());
                    player[0].nhanThemLaBai(lstMangBaLB.get(3).getC2());
                    player[0].nhanThemLaBai(lstMangBaLB.get(3).getC3());
                    player[3].nhanThemLaBai(lstMangBaLB.get(0).getC1());
                    player[3].nhanThemLaBai(lstMangBaLB.get(0).getC2());
                    player[3].nhanThemLaBai(lstMangBaLB.get(0).getC3());
                    
                    player[1].nhanThemLaBai(lstMangBaLB.get(2).getC1());
                    player[1].nhanThemLaBai(lstMangBaLB.get(2).getC2());
                    player[1].nhanThemLaBai(lstMangBaLB.get(2).getC3());
                    player[2].nhanThemLaBai(lstMangBaLB.get(1).getC1());
                    player[2].nhanThemLaBai(lstMangBaLB.get(1).getC2());
                    player[2].nhanThemLaBai(lstMangBaLB.get(1).getC3());
                    break;

                case 2:
                    player[0].nhanThemLaBai(lstMangBaLB.get(2).getC1());
                    player[0].nhanThemLaBai(lstMangBaLB.get(2).getC2());
                    player[0].nhanThemLaBai(lstMangBaLB.get(2).getC3());
                    player[2].nhanThemLaBai(lstMangBaLB.get(0).getC1());
                    player[2].nhanThemLaBai(lstMangBaLB.get(0).getC2());
                    player[2].nhanThemLaBai(lstMangBaLB.get(0).getC3());
                    
                    player[1].nhanThemLaBai(lstMangBaLB.get(3).getC1());
                    player[1].nhanThemLaBai(lstMangBaLB.get(3).getC2());
                    player[1].nhanThemLaBai(lstMangBaLB.get(3).getC3());
                    player[3].nhanThemLaBai(lstMangBaLB.get(1).getC1());
                    player[3].nhanThemLaBai(lstMangBaLB.get(1).getC2());
                    player[3].nhanThemLaBai(lstMangBaLB.get(1).getC3());
                    break;

                case 3:
                    // Khong doi
                    break;
            }

            mangBaLB.clear();
            player[0].sortBoBai();
            for (int i = 0; i <= 2; i++) {
                player[i + 1].sortBoBai();
            }

        }
        TimNguoiDiDauTien();
        if (currentTurn != 0) {
            // Computer di 2 chuon
            arrayId4Card.add(player[currentTurn].di2Chuong());
            hienTatCaLaBaiChay();
            try {
                Thread.sleep(delay);
            } catch (Exception e) {
            }
            flagDi2Chuong = false;
            nextturn();
        }
        hienTatCaLaBaiChay();
        btnXuLyChung.setVisible(false);
        playState = KhaiBao.PLAYPLAYING;
        computerPlayPoker();
    }
    
    //-------------------------------------------------
    //-- chia bài
    //-- khởi tạo mảng 53 lá bài 1->53
    //-- chia bài xong sortBoBai lại theo nước cờ
    //-------------------------------------------------
    private void chiaBai() 
    {
        int[] ddau = new int[53];
        for (int i = 1; i < 53; i++) {
            ddau[i] = 0;
        }
        // tao ngau nhien mang tu 1-53
        SecureRandom numGenerate = new SecureRandom();
        int tam = numGenerate.nextInt(53);
        int p = 0;
        btnXuLyChung.setVisible(false);
        for (int i = 1; i < 53; i++) {
            while ((tam == 0) || (ddau[tam] == 1)) {
                tam = numGenerate.nextInt(53);
            }
            // danh dau la da co
            ddau[tam] = 1;

            // chia bai cho  nguoi choi
            
            try {
                player[p].nhanThemLaBai(tam);
                p = (p + 1) % 4;
            } catch (Exception e) {
            }
            hienTatCaLaBaiChay();
            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }
        }
        for (int i = 0; i < 4; i++) {
            player[i].sortBoBai(); // rip adat
        }
        hienTatCaLaBaiChay();
        btnXuLyChung.setVisible(true);

        System.out.println("Chia bài xong !!! ");
    }

    public void newRound() 
    {
        for(int i = 0; i < 4; ++i)
        {
            player[i].newRound();
        }
        chiaBai();
        for(int i = 0; i < 4; ++i)
        {
            player[i].sortBoBai(); //rip a dat
        }
        flagChonCo = false;
        count13Card = 0;
        showbutton("Đổi bài");
        hienTatCaLaBaiChay();
    }

    public void newGame() {
        for (int i = 0 ; i < 4; ++i)
        {
            player[i].resetDiem();
        }
        arrayId4Card.clear();
        newRound();
    }
}

